﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Validation.sp2010.Classes;
using Validation.sp2010.Helper;

namespace Validation.sp2010.ControlTemplates.Validation.sp2010
{
    public partial class FormulaText : UserControl
    {
        #region public properties
        public List<SPField> Fields { get; set; }

        public string DisplayFinalString
        {
            get
            {
                return Text.Text;
            }
        }

        public string InternalFinalString
        {
            get
            {
                string result = "";
                string display = this.DisplayFinalString;
                int cmp1 = 0;
                int cmp2 = 0;
                int cmp = 0;

                bool inSurround = false;

                int length = display.Length;
                while (cmp < length)
                {
                    if (display[cmp] == Constante.SurroundC1)
                    {
                        cmp1 = cmp;
                        result += display[cmp];
                        inSurround = true;
                    }
                    else
                    {
                        if (display[cmp] == Constante.SurroundC2)
                        {
                            //closed
                            cmp2 = cmp - 1;
                            string displayColumn = display.Substring(cmp1 + 1, cmp2 - cmp1);
                            result += Columns.Items.FindByText(displayColumn).Value;
                            result += display[cmp];
                            inSurround = false;
                        }
                        else
                        {
                            //normal
                            if (!inSurround)
                                result += display[cmp];
                        }
                    }
                    cmp++;
                }


                return result;
            }
        }

        public bool VerifyFormule()
        {
            if (string.IsNullOrEmpty(DisplayFinalString))
            {
                _error = HelperUtilities.GetResources("error_required", SPContext.Current.Web.Language);
                return false;
            }
            
            bool syntax = HelperParser.VerifyFormule(DisplayFinalString);
            if (!syntax)
            {
                _error = HelperUtilities.GetResources("error_formule_syntax", SPContext.Current.Web.Language);
                return false;
            }

            List<string> columns = HelperParser.GetStringsFormula(DisplayFinalString);
            foreach (string c in columns)
            {
                if (Columns.Items.FindByText(c) == null)
                {
                    _error =string.Format(HelperUtilities.GetResources("error_formule_column", SPContext.Current.Web.Language),c);
                    return false;
                }
            }
            _error = "";
            return true;
        }

        
        #endregion

        private string _error
        {
            set { LabelError.Text = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ViewState["loaded"] == null)
            {
                Columns.DataSource = this.Fields;
                Columns.DataBind();

                ViewState["loaded"] = true;
            }


        }

        protected void left_Click(object sender, EventArgs e)
        {
            Text.Text += string.Format(Constante.SurroundFieldFormat, Columns.SelectedItem.Text);
        }

    }
}
